package com.codebase.zookeeper.curator;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.junit.Test;

/**
 * 连接
 */
public class Connect {

    /**
     * curator创建连接会话
     */
    @Test
    public void test() throws Exception{
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
        CuratorFramework client = CuratorFrameworkFactory.newClient("127.0.0.1:2181", 5000, 3000, retryPolicy);
        client.start();
        Thread.sleep(1000);
    }

    /**
     * fluent 风格创建 且设置 命名空间
     * namespace： 指定一个zookeeper的根目录
     * @throws Exception
     */
    @Test
    public void test1() throws Exception{
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
        CuratorFramework client = CuratorFrameworkFactory.builder()
                .connectString("127.0.0.1:2181")
                .sessionTimeoutMs(5000)
                .retryPolicy(retryPolicy)
                .namespace("/root").build();
        client.start();
        Thread.sleep(1000);

    }
}
